/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jate;

import jate.core.Yate;
import jate.core.YateMessage;
import java.io.IOException;
import java.util.UUID;

/**
 *
 * @author milan
 */
public class Main {

    static String ourcallid = "";
    static String partycallid = "";
    static String state = "";
    static String dir = "";
    static YateMessage currentEvent = null;

    public static void init() {
        Main.ourcallid = "playrec/" + UUID.randomUUID().toString();
        Main.partycallid = "";
        Main.state = "call";
        Main.dir = "/tmp";

        Yate.init();
        Yate.debugOn = true;
        /* Install handlers for the DTMF and wave EOF messages */
        Yate.install("chan.dtmf");
        Yate.install("chan.notify");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Main.init();
        while(!Main.state.equals("")){
            try{
                Main.currentEvent = Yate.getMessage();
            }
            catch(IOException exception){
                Yate.output(exception.getMessage());
                break;
            }
            if(Main.currentEvent == null){
                Yate.output("Event is null break...");
                break;
            }
            else if(Main.currentEvent.name.equals("empty")){
                continue;
            }

            if(Main.currentEvent.type.equals("incoming")){
                if(Main.currentEvent.name.equals("call.execute")){
                    Main.partycallid = Main.currentEvent.params.get("id");
                    Main.currentEvent.params.put("targetid", Main.ourcallid);
                    Main.currentEvent.handled = true;
                    // we must ACK this message before dispatching a call.answered
                    try{
                        Yate.acknowledge(Main.currentEvent);
                    }
                    catch(Exception e){
                        Yate.output(e.getMessage());
                    }
		    // we already ACKed this message
		    Main.currentEvent = null;
                    //answer the call
                    YateMessage msg = new YateMessage("call.answered");
                    msg.params.put("id", Main.ourcallid);
                    msg.params.put("targetid", Main.partycallid);
                    try{
                        Yate.dispatch(msg);
                    }
                    catch(Exception e){
                        Yate.output(e.getMessage());
                    }
                    //attach prompt
                    msg = new YateMessage("chan.attach");
                    //msg.params.put("id", Main.ourcallid);
                    //msg.params.put("targetid", Main.partycallid);
                    msg.params.put("source", "wave/play//home/sln_prompts/vas_prompts/ivr_online_payment_de/hold_the_line_until_charged_completely_de.slin");
                    try{
                        Yate.dispatch(msg);
                    }
                    catch(Exception e){
                        Yate.output(e.getMessage());
                    }

                }
                if (Main.currentEvent != null){
                    try{
                        Yate.acknowledge(Main.currentEvent);
                    }
                    catch(Exception e){
                        Yate.output(e.getMessage());
                    }
                }
            }
        }
    }
}
